import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;


public class Console {
	private static FTPClient client = new FTPClient();
	
	public static void main(String[] args) throws IOException {  
		int port = 5298;
		String IP;
		if (args.length == 0){
			 IP = "localhost";
		}	
		else{
			 IP = args[0];
		}	
		if (client.connect(IP, port)){
			while(true)
				readCommand();
		}	
	}

	public static void readCommand() throws IOException{
		BufferedReader inStr = new BufferedReader(new InputStreamReader(System.in));
		String command, parameter;
		String[] tokens = inStr.readLine().split(" ");
		if (tokens.length == 0 ){
			System.out.println("Comando nulo!");
			return;
		}
		if (tokens.length > 2){
			System.out.println("Excesso de Parametros!");
			return;
		}	 
		else{
			command = tokens[0];
			if (command.equals("ls")){
				client.executeLS();
				return;
			}
			else if (tokens.length == 2){
				parameter = tokens[1];
				if (command.equals("get")){
					client.executeGET(parameter);
					return;
				}
				if (command.equals("put")){
					client.executePUT(parameter);
					return;
				}
				if (command.equals("cd")){
					client.executeCD(parameter);
					return;
				}
			}
			else{
				if(command.equals("get") || command.equals("put") || command.equals("cd"))
					System.out.println("Falta de Parametros!");
				else
					System.out.println("Erro de Sintaxe.Comandos disponiveis:\nls\nget<nomearquivo>\nget<nomearquivo>\ncd <diremoto>");
				return;
			}
		}	 
	}
}
